package com.lanyou.esb.cook.proxy.manager;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lanyou.esb.cook.proxy.dao.impl.FieldSystemDao;
import com.lanyou.esb.cook.proxy.dao.impl.HttpDao;
import com.lanyou.esb.cook.proxy.entity.FieldSystem;
import com.lanyou.esb.cook.proxy.entity.Http;

/**
 * Created by gonglei on 16/4/1.
 */
public class FieldSystemManager {

	private Map<String, FieldSystem> systemMap;

	private FieldSystemDao fieldSystemDao;

	private HttpDao httpDao;

	public FieldSystemManager() {
		systemMap = new HashMap<>();
	}

	public FieldSystemDao getFieldSystemDao() {
		return fieldSystemDao;
	}

	public void setFieldSystemDao(FieldSystemDao fieldSystemDao) {
		this.fieldSystemDao = fieldSystemDao;
	}

	/**
	 * 刷新所有系统接口
	 */
	public void reloadFieldSystems() {
		this.destroy();
		systemMap.clear();
		List<FieldSystem> systems = fieldSystemDao.findAll();
//		for (FieldSystem system : systems) {
//			system.setInterfaceMap(loadSystemInterfaces(system.getId()));
//			systemMap.put(system.getCode(), system);
//		}
	}

	/**
	 * 刷新单个系统接口
	 * 
	 * @param systemId
	 */
	public FieldSystem reloadFieldSystemById(String systemId) {
		FieldSystem system = fieldSystemDao.findById(systemId);
		if (system != null) {
			removeFieldSystem(system.getCode());
			//system.setInterfaceMap(loadSystemInterfaces(system.getId()));
			systemMap.put(system.getCode(), system);
		}
		return system;
	}

	public FieldSystem reloadFieldSystemByCode(String systemCode) {
		FieldSystem system = fieldSystemDao.findByCode(systemCode);
		if (system != null) {
			removeFieldSystem(system.getCode());
			//system.setInterfaceMap(loadSystemInterfaces(system.getId()));
			systemMap.put(systemCode, system);
		}
		return system;
	}

	/**
	 * 加载单个系统所有的接口
	 * 
	 * @param systemId
	 * @return
	 */
	private Map<String, Map<String, Http>> loadSystemInterfaces(String systemId) {
//		List<Http> interfaces = httpDao.getInterfacesBySystemId(systemId);
//		Map<String, Map<String, Http>> interfaceMap = new HashMap<>();
//		Map<String, Http> groupByVersion = null;
//		for (Http httpInterface : interfaces) {
//			groupByVersion = interfaceMap.get(httpInterface.getCode());
//			if (groupByVersion == null) {
//				groupByVersion = new HashMap<>();
//				interfaceMap.put(httpInterface.getCode(), groupByVersion);
//			}
//			groupByVersion.put(httpInterface.getVersion(), httpInterface);
//		}
//		return interfaceMap;
		return null;
	}

	public FieldSystem getFieldSystem(String systemCode) {
		return systemMap.get(systemCode);
	}

	public Collection<FieldSystem> getAllFieldSystems() {
		return systemMap.values();
	}

	/**
	 * 从缓存中删除一个系统
	 * 
	 * @param systemCode
	 */
	public void removeFieldSystem(String systemCode) {
		FieldSystem fieldSystem = systemMap.get(systemCode);
		if (fieldSystem != null) {
			//fieldSystem.destroy();
			systemMap.remove(systemCode);
		}
	}

	public void destroy() {
		for (FieldSystem fieldSystem : systemMap.values()) {
			//fieldSystem.destroy();
		}
	}

	public HttpDao getHttpDao() {
		return httpDao;
	}

	public void setHttpDao(HttpDao httpDao) {
		this.httpDao = httpDao;
	}
}
